package pos.dato;

import java.sql.*;

import pos.dominios.Comentario;

public class JDBCComentarioDAO implements IComentarioDAO{

    public void delete(Connection conn, String comentarioID) {
        String sql = "DELETE FROM Comentarios WHERE (comentarioID = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, comentarioID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
    }

    public void insert(Connection conn, String comentarioID, Comentario c, String propuestaID) {
        String sql = "INSERT INTO Comentarios ( comentarioID, propuestaID, titulo, opinion) VALUES (?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, comentarioID);
            stmt.setString(4, c.getTitulo());
            stmt.setString(5, c.getOpinion());
            stmt.setString(2, propuestaID);
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

    }

    public Comentario select(Connection conn, String comentarioID) {
        String sql = "SELECT * FROM Comentarios WHERE (comentarioID = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Comentario c = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, comentarioID);

            result = stmt.executeQuery();

            result.next();
            c = new Comentario();
            c.setTitulo(result.getString("titulo"));
            c.setOpinion(result.getString("opinion"));
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return c;
    }


}
